/*
 * Copyright (C) 2012 rothens
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package rpg.gfx;

import java.awt.image.BufferedImage;
import rpg.util.ImageUtil;

/**
 *
 * @author rothens
 */
public class EightPatch {

    private int leftcol;
    private int rightcol;
    private int toprow;
    private int bottomrow;
    private Bitmap[] bitmaps = new Bitmap[8];
    private int inset;
    /*
     * 012
     * 3 4
     * 567
     */

    public EightPatch(BufferedImage image, int leftcol, int rightcol, int toprow, int bottomrow, int inset) {
        this.leftcol = leftcol;
        this.rightcol = rightcol;
        this.toprow = toprow;
        this.bottomrow = bottomrow;
        this.inset = inset;

        bitmaps[0] = ImageUtil.getSub(image, 0, 0, leftcol, toprow);
        bitmaps[2] = ImageUtil.getSub(image, image.getWidth() - rightcol, 0, rightcol, toprow);
        bitmaps[5] = ImageUtil.getSub(image, 0, image.getHeight() - bottomrow, leftcol, bottomrow);
        bitmaps[7] = ImageUtil.getSub(image, image.getWidth() - rightcol, image.getHeight() - bottomrow, rightcol, bottomrow);
        int hor = image.getWidth() - leftcol - rightcol;
        int ver = image.getHeight() - toprow - bottomrow;
        bitmaps[1] = ImageUtil.getSub(image, leftcol, 0, hor, toprow);
        bitmaps[3] = ImageUtil.getSub(image, 0, toprow, leftcol, ver);
        bitmaps[4] = ImageUtil.getSub(image, image.getWidth() - rightcol, toprow, rightcol, ver);
        bitmaps[6] = ImageUtil.getSub(image, leftcol, image.getHeight() - bottomrow, hor, bottomrow);
    }

    public Bitmap overlay(Bitmap b) {
        int horW = b.width - leftcol - rightcol;
        int verH = b.height - toprow - bottomrow;
        if (horW < 0 || verH < 0) {
            return b;
        }
        b.inset(inset);
        /*draw corners*/
        b.drawOn(bitmaps[0], 0, 0);
        b.drawOn(bitmaps[2], b.width-rightcol, 0);
        b.drawOn(bitmaps[5], 0, b.height-bottomrow);
        b.drawOn(bitmaps[7], b.width-rightcol, b.height-bottomrow);
        /*draw sides*/
        b.drawOn(new Bitmap(horW, toprow, bitmaps[1]),leftcol,0);
        b.drawOn(new Bitmap(horW, bottomrow, bitmaps[6]),leftcol,b.height-bottomrow);
        b.drawOn(new Bitmap(leftcol, verH, bitmaps[3]), 0, toprow);
        b.drawOn(new Bitmap(rightcol, verH, bitmaps[4]), b.width-rightcol, toprow);
        
        return b;
    }

    public int getLeftcol() {
        return leftcol;
    }

    public void setLeftcol(int leftcol) {
        this.leftcol = leftcol;
    }

    public int getRightcol() {
        return rightcol;
    }

    public void setRightcol(int rightcol) {
        this.rightcol = rightcol;
    }

    public int getToprow() {
        return toprow;
    }

    public void setToprow(int toprow) {
        this.toprow = toprow;
    }

    public int getBottomrow() {
        return bottomrow;
    }

    public void setBottomrow(int bottomrow) {
        this.bottomrow = bottomrow;
    }
    
    
}
